var bookdata = require("bookdata");
cc.Class({
    extends: cc.Component,

    properties: {
        //临时
        p1: {
            default: null,
            type: cc.Node
        },
        p2: {
            default: null,
            type: cc.Node
        },
        p3: {
            default: null,
            type: cc.Node
        },
        p4: {
            default: null,
            type: cc.Node
        },
        p5: {
            default: null,
            type: cc.Node
        },
        common: {
            default: null,
            type: cc.Node
        },
        menulist: {
            default: null,
            type: cc.Node
        },
        label_prefab: cc.Prefab

    },

    // use this for initialization
    onLoad: function() {
        cc.game.addPersistRootNode(this.common);
        var self = this;
        var common = cc.find('common');
        this.menulist.removeAllChildren(true);
        // cc.loader.loadRes('prefab/p1', function(err, prefab) {
        for (var i = 0; i < bookdata.length; i++) {
            let bname = bookdata[i].bname;
            let readcount = cc.sys.localStorage.getItem(bname + '_readcount') || '0';
            cc.log(this.label_prefab);
            let newNode = cc.instantiate(this.label_prefab);
            cc.log(newNode);
            newNode.getComponent(cc.Label).string = readcount + "/" + bookdata[i].pages.length + ' ' + bname;
            newNode.mindex = i;
            newNode.page = readcount;
            newNode.on(cc.Node.EventType.TOUCH_END, function(event) {
                common.current_index = this.mindex;
                common.current_page = this.page;
                cc.director.loadScene("book");
            });
            newNode.parent = self.menulist;
        }

        // });

        // this.p1.on(cc.Node.EventType.TOUCH_END, function(event) {
        //     common.current_index = 0;
        //     cc.director.loadScene("book");
        // });
        // this.p2.on(cc.Node.EventType.TOUCH_END, function(event) {
        //     common.current_index = 1;
        //     cc.director.loadScene("book");
        // });
        // this.p3.on(cc.Node.EventType.TOUCH_END, function(event) {
        //     common.current_index = 2;
        //     cc.director.loadScene("book");
        // });
        // this.p4.on(cc.Node.EventType.TOUCH_END, function(event) {
        //     common.current_index = 3;
        //     cc.director.loadScene("book");
        // });
        // this.p5.on(cc.Node.EventType.TOUCH_END, function(event) {
        //     common.current_index = 4;
        //     cc.director.loadScene("book");
        // });

        //  this.p1.string=readcount+"/"+bookdata[0].pages.length+this.p1.string;
    },
    start: function() {
        //这里考虑使用localstroage来进行保存，显示哪些读过了，读到了哪里了
        //cc.log(bookdata);
        let bname = bookdata[0].bname;
        let readcount = cc.sys.localStorage.getItem(bname + '_readcount') || '0';
        cc.log(readcount + this.p1.string);
        this.p1.start = function() {
            cc.log(this.string);
            this.string = readcount + "/" + bookdata[0].pages.length + this.string;
        }
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});